package com.atguigu.yygh.shop.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author cs
 * @create 2021/3/22
 * @desc
 */
@Component
public class String2DateConverter implements Converter<String, Date> {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public Date convert(String str) {
        if(str == null || str.isEmpty()) {
            return null;
        }
        java.util.Date temp = null;
        try {
            temp = sdf.parse(str);
        } catch (ParseException e) {
            return null;
        }
        return (temp != null) ? new java.util.Date(temp.getTime()) : null;
    }
}
